//
//  SignMainController.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/26.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SignMainController.h"
#import "SignMainView.h"
#import "SignDataCenter.h"
#import "PhotoBrowerView.h"

@interface SignMainController ()<SignMainViewDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *imagepickController;

@property (nonatomic, strong) SignMainView *signMainView;
@property (nonatomic, strong) SignDataCenter *dataCenter;
@property (nonatomic, assign) BOOL isReload;

@end

@implementation SignMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.rightBtnTitle = @"返回";
    
    _isReload = NO;

    _dataCenter = [SignDataCenter new];
    
    [self makeSignMainView];
    
    [self loadTaskInfoData];
    
    [self loadSignListData];
    
    [self initImagePicker];
}

- (void)makeSignMainView
{
    _signMainView = [[SignMainView alloc] init];
    _signMainView.delegate = self;
    [self.baseView addSubview:_signMainView];
    [_signMainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.bottom.mas_equalTo(-44);
    }];
}

#pragma mark --- 实例化相机
-(void)initImagePicker
{
    //绑定调用相册的协议方法
    if (self.imagepickController == nil) {
        self.imagepickController = [[UIImagePickerController alloc]init];
        [self.imagepickController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
        
        [self.imagepickController.navigationBar setTitleTextAttributes:
         @{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],
           NSForegroundColorAttributeName:[UIColor blackColor]}];
        [self.imagepickController.navigationBar setTintColor:[UIColor blackColor]];
        self.imagepickController.delegate = self;
    }
}

- (void)loadTaskInfoData
{
    [_signMainView loadTaskInfo:self.taskModel];
}

- (void)loadSignListData
{
    NSString *loadingInfo = _isReload == YES ? @"重新加载..." : @"正在加载...";

    [YJProgressHud showLoadingHud:loadingInfo];
    [_dataCenter fetchSignList:self.taskModel.number complation:^(id data, NSString *error) {
       
        if (error == nil) {
            [YJProgressHud dissmiss];
            _signMainView.signData = data;
        }else{
            [YJProgressHud showErrorHud:error];
        }
        
    }];
}

- (void)clickSignIn
{
    [self signImageUploadAction];
}

- (void)signImageUploadAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择一张图片作为头像" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            self.imagepickController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
        }
    }];
    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        if (self.imagepickController != nil) {
            self.imagepickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagepickController animated:YES completion:nil];
        }
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [alertController addAction:archiveAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark --- imagePickerViewDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = nil;
    if(picker.allowsEditing == NO){
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }else{
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    }
    if(image != nil){
        
        [YJProgressHud showLoadingHud:@"打卡中..."];

        UIImage *scaleImage = [UIImage scaleImage:image multiple:0.25];
        
        NSData *imageData = UIImageJPEGRepresentation(scaleImage, 0.25);
        
        __weak __typeof(self) weakSelf = self;
        [_dataCenter signinWithTask:self.taskModel.number signImage:[UIImage imageWithData:imageData] complation:^(NSString *error) {
            if (error == nil) {
                [YJProgressHud showSuccessHud:@"打卡成功"];
                _isReload = YES;
                [weakSelf loadSignListData];
            }else{
                [YJProgressHud showErrorHud:error];
            }
        }];
        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)didSelectSignModel:(SignModel *)signModel
{
    if (signModel.issign == YES) {
        [[PhotoBrowerView sharedBrower] showBrower:signModel];
    }else{
        [YJProgressHud showErrorHud:@"这天没有签到过哦~~~"];
    }
    
}

- (void)clickRightButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
